<?php
Yii::import('application.widgets.chart.AbstractExtJsChart');

abstract  class AbstractPieChart extends AbstractExtJsChart{


	public function run(){
//		$fields=$this->getFields();
//		$fieldsJson=CJSON::encode($fields);
//		$allFieldsJson=CJSON::encode(CMap::mergeArray(array('month'),$fields ));
		$data=$this->getData();
		$dataJson=CJSON::encode($data);
		$js=<<<EOF
Ext.onReady(function () {
    var store = Ext.create('Ext.data.JsonStore', {
        fields: ['name','value'],
        data: {$dataJson}
    });

    var panel1 = Ext.create('widget.panel', {
        width: {$this->width},
        height: {$this->height},
        title: '{$this->title}',
        renderTo: '{$this->getId()}',
        layout: 'fit',
        items: {
            xtype: 'chart',
            animate: true,
            shadow: true,
            store: store,
            legend: {
                position: 'bottom'
            },
            theme: 'Base:gradients',
            insetPadding: 30,
            series: [{
                type: 'pie',
                field: 'value',
                showInLegend: true,
                donut: false,
                tips: {
                  trackMouse: true,
                  width: 180,
                  height: 28,
                  renderer: function(storeItem, item) {
                    //calculate percentage.
                    var total = 0.0;
                    store.each(function(rec) {
                        total += rec.get('value');
                    });
                    this.setTitle(storeItem.get('name') + ': '+storeItem.get('value')+' (' + Math.round((0.0+storeItem.get('value')) / total * 100) + '%)');
                  }
                },
                highlight: {
                  segment: {
                    margin: 20
                  }
                },
                label: {
                    field: 'name',
                    display: 'rotate',
                    contrast: true,
                    font: '18px Arial'
                }
            }]
        }
    });
});	
EOF;
		$this->registerChartScript($js);
		parent::run();
	}
	
	/**
	 * should return array in array(array('name'=>$name, 'value'=>$value) ....) format
	 */
	protected abstract function getData();
}